package com.fansl.koala.quickdev.component.message.email;

import com.fansl.koala.quickdev.common.bean.entity.SendMailMessage;
import com.fansl.koala.quickdev.common.bean.po.SendMailMessagePo;
import com.fansl.koala.quickdev.common.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fansl
 * @Description: 邮箱消息模板生成策略
 * @date 2020/3/18 17:18
 */
@Slf4j
public class EmailMessageTemplateContext {

    private EmailMessageTemplateExecutor emailMessageTemplateExecutor;
    private final SendMailMessagePo mailMessage;

    public EmailMessageTemplateContext(SendMailMessagePo mailMessage) {
        this.mailMessage = mailMessage;
        builder();
    }

    private void builder() {
        switch (mailMessage.getEmailMessageType()) {
            case REGISTER:
                emailMessageTemplateExecutor = SpringContextHolder.getBean(RegisterMessageTemplateExecutor.class);
                break;
            case ACCOUNT_ACTIVATION:
                emailMessageTemplateExecutor = SpringContextHolder.getBean(AccountActivationTemplateExecutor.class);
                break;
            case COMMON:
            default:
                break;
        }
    }

    public SendMailMessage execute() throws Exception {
        if (emailMessageTemplateExecutor != null) {
            return emailMessageTemplateExecutor.createMessageTemplate(mailMessage);
        }
        return null;
    }
}
